Series 

The following examples show the use of the EViews programming language to perform the following tasks:

Calculate cumulative sums

EViews 6 and later has a built-in function for calculating cumulative sums. However, you could also easily calculate such sums with a few lines of commands.

Say we have a series X for which we would like to calculate the cumulative sum. If the series does not contain NAs, you can use:

 
smpl @first @first
series sum0 = x
smpl @first+1 @last
sum0 = sum0(-1) + x

If the series does contain NAs, you have two options, depending on how you would like the NAs to be treated. If you would like to continue accumulating the sum, ignoring the NA observations, you can use:

 
smpl @first @first
series sum1 = @nan(x,0)
smpl @first+1 @last
sum1 = sum1(-1) + @nan(x,0)
smpl @all if x = na
sum1 = na

If you would like to reset the sum to zero whenever an NA appears in the series you can use:

 
smpl @all
series sum2 = x
smpl if sum2(-1)<>na and x<>na
sum2 = sum2(-1) + x

The complete program is as follows:

 
' three rules to compute cumulative sums with missing values
 
'create workfile
wfcreate cumsum u 1 10
 
'fill data
series x
x.fill 4,2,na,4,1,3,2,na,7,5
 
'for series without NAs
smpl @first @first
series sum0 = x
smpl @first+1 @last
sum0 = sum0(-1) + x
 
'ignore NAs
smpl @first @first
series sum1 = @nan(x,0)
smpl @first+1 @last
sum1 = sum1(-1) + @nan(x,0)
smpl @all if x = na
sum1 = na
 
'reset to zero at NAs
smpl @all
series sum2 = x
smpl if sum2(-1)<>na and x<>na
sum2 = sum2(-1) + x
 
smpl @all
show x sum0 sum1 sum2

The following sample output shows the differences between the three methods:

                           

^Top

Compute lags on a broken sample of observations

Many operations in EViews can be performed on a subset of the workfile observations using the smpl command. To estimate an equation using only observations which have a positive value of X, you could use: 

 
smpl if x>0
eq1.ls y c x

Time series operations often rely on observations being adjacent, so that this method may not work as expected. In this example we demonstrate a general technique for working with a subset of observations from the workfile as though they were adjacent. There are three steps to using the method: 

Consider the simple case of creating a series which contains the differences between consecutive positive values of the series X, ignoring any intervening negative values. 

The commands:

 
smpl if x>=0
series dxp = d(x)

will not compute the desired values, since the d function computes differences between adjacent values, not between successive values in the sample.

The SUBSET.PRG program solves this problem using the general technique outlined above. The solution is stored in the series DX. The series X_S and DX_S are left in the workfile so you can follow the intermediate calculations.

 
' time series operation on subsamples
 
' create workfile
wfcreate subset u 1 10
 
' fill data
series x
x.fill 4,2,na,4,1,3,2,na,7,5
 
' create sample
sample ss if x<>NA and x>=0
 
' create short x series
smpl @all
vector temp  
stomna(x, temp, ss)
series x_s
mtos(temp, x_s)
!rows = @rows(temp)  'save number of elements
delete temp
 
' difference short x series
series dx_s = d(x_s)
 
' map back into long series dx
vector temp
stomna(dx_s, temp)
vector(!rows) temp  'trim to number of elements
series dx
mtos(temp, dx, ss)
delete temp
 
' display results
show x dx x_s dx_s

^Top

Create dummy variables with a loop

It is sometimes useful to create a set of dummy variables for a range of observations in a workfile. You can then include one or more of these dummies in an estimation simply by adding their names to the list of exogenous variables. It is easy to create these series by hand, though somewhat tedious.

The program MAKE_DUM.PRG automates this task. It should be run from the command line with arguments for the first and the last observation for which to create dummies. For example, to create dummies in a quarterly workfile from the first quarter of 1982 to the last quarter of 1983, the command would be:

 
run make_dum 1982:1 1983:4

As is, the program should work for annual, semiannual, quarterly, monthly and undated workfiles. It should be modified for weekly and daily workfiles.

Note that from version 6 of EViews onwards, you could create such dummy variables more easily using the @expand command.

 
' create dummy variables for every observation in sample
 
'create workfile
wfcreate dumtest q 1970 1990
 
'set up start and end dates
%start = "1972:1" 'first observation to dummy
%end = "1979:4"           'last observation to dummy
 
'generate dummy variables from 'start' to 'end'
for !i = @dtoo(%start) to @dtoo(%end)
   'string containing observation offset
   %obsstr = @otod(!i)
   'name of dummy
   if (@mid(%obsstr, 5, 1) = ":") then
          %name = "d_" + @left(%obsstr, 4) + "_" + @mid(%obsstr, 6)
   else
          %name = "d_" + %obsstr
   endif
 
   'generate dummy series
   smpl @all
   series {%name} = 0
   smpl {%obsstr} {%obsstr}
   series {%name} = 1
next
 
smpl @all
show d_*

^Top